//
//  TodaySignView.m
//  sooc-ios_new
//
//  Created by houshijia on 15/12/23.
//  Copyright © 2015年 xuguangChao. All rights reserved.
//

#import "TodaySignView.h"

@implementation TodaySignView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 5;
        self.backgroundColor = [UIColor whiteColor];
        //
        _kcmcLabel = [[UILabel alloc]init];
        _kcmcLabel.text= @"高等数学";
        _kcmcLabel.numberOfLines = 1;
        _kcmcLabel.font = NormalFont(42);
        _kcmcLabel.textColor = Color333333;
        [self addSubview:_kcmcLabel];
        [_kcmcLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top).offset(5);
            make.left.equalTo(self.mas_left).offset(15);
            make.right.equalTo(self.mas_right).offset(-15);
            make.height.mas_equalTo(@35);
        }];
        
        //
        _lsImgView = [[UIImageView alloc]init];
      
        _lsImgView.image = [UIImage imageNamed:@"我的-课程发布人"];
        [_lsImgView setContentMode:UIViewContentModeScaleToFill];
        [self addSubview:_lsImgView];
        [_lsImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_kcmcLabel.mas_bottom).offset(0);
            make.left.equalTo(_kcmcLabel);
            make.height.mas_equalTo(@(ScaleSize(48)));
            make.width.mas_equalTo(@(ScaleSize(48)));
            
        }];
        
        
       
        _lsmcLabel = [[UILabel alloc]init];
        _lsmcLabel.text = @"马未都";
        _lsmcLabel.textAlignment = NSTextAlignmentLeft;
        _lsmcLabel.textColor = Color999999;
        _lsmcLabel.font = NormalFont(42);
        [self addSubview:_lsmcLabel];
        [_lsmcLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_kcmcLabel.mas_bottom).offset(0);
            make.left.equalTo(_lsImgView.mas_right).offset(5);
            make.height.equalTo(_lsImgView);
            make.width.mas_equalTo(@100);
            
        }];
        //
        _skrqLabel = [[UILabel alloc]init];
        _skrqLabel.text = @"2015-11-11 10:41";
        _skrqLabel.textAlignment = NSTextAlignmentRight;
        _skrqLabel.textColor = Color999999;
//        _skrqLabel.backgroundColor = [UIColor orangeColor];
        _skrqLabel.font = NormalFont(39);
        [self addSubview:_skrqLabel];
        [_skrqLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.and.height.equalTo(_lsmcLabel);
            make.right.equalTo(self.mas_right).offset(-ScaleSize(55));
            make.width.mas_equalTo(@120);
        }];
        //
        
        _line1 = [[UIView alloc]initWithFrame:CGRectMake(0, 57+ScaleSize(33), self.frame.size.width, 0.5)];
        _line1.backgroundColor = colordbdbdb;
        [self addSubview:_line1];
//        [_line1 mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(_lsImgView.mas_bottom).offset(10);
//            make.height.mas_equalTo(@1);
//            make.width.and.left.equalTo(self);
//
//        }];
        
        //
        _xsxmTF = [[DSTextField alloc]init];
        _xsxmTF.font = NormalFont(42);
        _xsxmTF.leftViewMode = UITextFieldViewModeAlways;
        _xsxmTF.borderStyle = UITextBorderStyleNone;
//        _xsxmTF.placeholder = @"必填";
//        _xsxmTF.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_xsxmTF];
        [_xsxmTF mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@45);
            make.top.equalTo(_line1.mas_bottom).offset(0);
            make.left.equalTo(_lsImgView.mas_left).offset(0);
            make.width.mas_equalTo(@(self.frame.size.width-ScaleSize(75)));
        }];
        UILabel *xm = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 55, 35)];
        xm.text = @"姓名:";
        xm.font = NormalFont(42);
        _xsxmTF.leftView = xm;
        xm.textAlignment = NSTextAlignmentCenter;
        
        UILabel *redStar0 = [[UILabel alloc]initWithFrame:CGRectMake(0, 5, 8, 30)];
        redStar0.textColor = [UIColor redColor];
        redStar0.text = @"*";
        [xm addSubview:redStar0];

        //
        _line2 = [[UIView alloc]init];
        _line2.backgroundColor= colordbdbdb;
//        _line2.alpha = 0.2;
        [self addSubview:_line2];
        [_line2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_xsxmTF.mas_bottom).offset(5);
            make.left.equalTo(self.mas_left).offset(15);
            make.right.equalTo(self.mas_right).offset(-15);
            make.height.mas_equalTo(@0.5);
            
        }];
        
        //
        _xsxhTF = [[DSTextField alloc]init];
//        _xsxhTF.placeholder = @"必填";
        UILabel *xh = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 55, 35)];
        xh.text = @"学号:";
        xh.textAlignment = NSTextAlignmentCenter;
        xh.font = NormalFont(42);
        
        UILabel *redStar1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 5, 8, 30)];
        redStar1.textColor = [UIColor redColor];
        redStar1.text = @"*";
        [xh addSubview:redStar1];
        _xsxhTF.font = NormalFont(42);
        _xsxhTF.leftViewMode = UITextFieldViewModeAlways;
        _xsxhTF.leftView = xh;
        _xsxhTF.borderStyle = UITextBorderStyleNone;
//        _xsxhTF.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_xsxhTF];
        [_xsxhTF mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_line2.mas_bottom).offset(0);
            make.left.and.width.and.height.equalTo(_xsxmTF);
        }];
        
        //
        _line3 = [[UIView alloc]init];
        _line3.backgroundColor= colordbdbdb;
//        _line3.alpha = 0.2;
        [self addSubview:_line3];
        [_line3 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_xsxhTF.mas_bottom).offset(5);
            make.left.equalTo(self.mas_left).offset(15);
            make.right.equalTo(self.mas_right).offset(-15);
            make.height.mas_equalTo(@0.5);
            
        }];
        
        //
        _bjTF = [[DSTextField alloc]init];
//        _bjTF.placeholder = @"必填";
        _bjTF.font = NormalFont(42);
        _bjTF.leftViewMode = UITextFieldViewModeAlways;
        _bjTF.borderStyle = UITextBorderStyleNone;
        UILabel *bj = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 55, 35)];
        bj.text = @"班级:";
        bj.textAlignment = NSTextAlignmentCenter;
        bj.font = [UIFont systemFontOfSize:15];
        
        UILabel *redStar2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 5, 8, 30)];
        redStar2.textColor = [UIColor redColor];
        redStar2.text = @"*";
        [bj addSubview:redStar2];
        _bjTF.leftView = bj;
//        _bjTF.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_bjTF];
        [_bjTF mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_line3.mas_bottom).offset(0);
            make.left.and.width.and.height.equalTo(_xsxhTF);
        }];
        
        //
        _line5 = [[UIView alloc]init];
        _line5.backgroundColor = colordbdbdb;
//        _line5.alpha = 0.2;
        [self addSubview:_line5];
        [_line5 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_bjTF.mas_bottom).offset(5);
            make.left.equalTo(self.mas_left).offset(15);
            make.right.equalTo(self.mas_right).offset(-15);
            make.height.mas_equalTo(@0.5);
        }];
        
        //
        
        _picBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        _picBtn.backgroundColor = [UIColor orangeColor];
        _picBtn.enabled = NO;
        [_picBtn addTarget:self action:@selector(upPic) forControlEvents:UIControlEventTouchUpInside];
        
        [_picBtn setTitle:@"课堂拍照" forState:UIControlStateNormal];
        
        _picBtn.titleLabel.font = NormalFont(42);
        
        [_picBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
//        [_picBtn setImage:img.image forState:UIControlStateNormal];
        
        [self addSubview:_picBtn];
        
        [_picBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_line5.mas_bottom).offset(10);
            make.left.equalTo(_lsImgView.mas_left);
            make.height.mas_equalTo(@40);
        }];
        
      
        _picImgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        _picImgBtn.backgroundColor =  [UIColor orangeColor];
        [_picImgBtn setImage:[UIImage imageNamed:@"课程签到-拍照上传"] forState:UIControlStateNormal];
        UIImage *highImage = [UIImage imageNamed:@"课程签到-拍照上传"];
        [_picImgBtn setImage:[self imageByApplyingAlpha:0.5 image:highImage] forState:UIControlStateHighlighted];
        
                [_picImgBtn.imageView setContentMode:UIViewContentModeScaleAspectFit];
        [_picImgBtn addTarget:self action:@selector(upPic) forControlEvents:UIControlEventTouchUpInside];

        [self addSubview:_picImgBtn];
        [_picImgBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_picBtn.mas_right).offset(5);
            make.top.equalTo(_picBtn.mas_top).offset(-5);
            make.bottom.equalTo(_picBtn.mas_bottom).offset(5);
            make.width.mas_equalTo(@40);
        }];
        
       
        
        
        //
        _line4 = [[UIView alloc]init];
        _line4.backgroundColor =  colordbdbdb;
//        _line4.alpha = 0.2;
        [self addSubview:_line4];
        [_line4 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_picBtn.mas_bottom).offset(10);
            
            make.left.and.right.and.height.equalTo(_line1);
        }];
        
        //拍到的照片
        _imgView = [[UIImageView alloc]init];
        _imgView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_imgView];
        [_imgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_line5.mas_bottom).offset(4);
            make.bottom.equalTo(_line4.mas_top).offset(-4);
            make.left.equalTo(_picImgBtn.mas_right).offset(5);
            make.width.mas_equalTo(@90);
        }];
        
        //
        _alert = [[UILabel alloc]init];
        _alert.text = @"请确认您的个人信息，否则老师找不到你，会算你旷课哦！";
        _alert.numberOfLines = 0;
        _alert.textColor = Color999999;
//        _alert.backgroundColor = [UIColor redColor];
        _alert.font = NormalFont(34);
        [self addSubview:_alert];
        [_alert mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_line4.mas_bottom).offset(ScaleSize(30));
            make.left.equalTo(_lsImgView.mas_left);
            make.right.equalTo(self.mas_right).offset(-5);
            make.height.mas_equalTo(@28);
            
        }];
     
        _backView = [[UIView alloc]init];
        _backView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_backView];
        [_backView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_alert.mas_bottom).offset(0);
            make.left.and.right.and.bottom.equalTo(self);
        }];
        
        _signBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _signBtn.layer.masksToBounds = YES;
        _signBtn.layer.cornerRadius = 2;
        
        [_signBtn addTarget:self action:@selector(clickSign:) forControlEvents:UIControlEventTouchUpInside];
//        _signBtn.backgroundColor = BGC;
        [_signBtn setBackgroundImage:[self imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
        [_signBtn setBackgroundImage:[self imageWithColor:BGC] forState:UIControlStateHighlighted];

        _signBtn.titleLabel.font = NormalFont(42);
        [_signBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_signBtn setTitleColor:Color1b97e9 forState:UIControlStateNormal];
        [_backView addSubview:_signBtn];
        [_signBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_backView.mas_top).offset(10);
//            make.centerX.equalTo(_backView);
            make.right.equalTo(_backView.mas_right).offset(-6);
            make.bottom.equalTo(_backView.mas_bottom).offset(-10);
            make.width.mas_equalTo(@(76));
        }];
    
    
    }
    return self;
    
    
    
}

- (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

- (UIImage *)imageByApplyingAlpha:(CGFloat)alpha  image:(UIImage*)image

{
    
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0f);
    
    
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    CGRect area = CGRectMake(0, 0, image.size.width, image.size.height);
    
    
    
    CGContextScaleCTM(ctx, 1, -1);
    
    CGContextTranslateCTM(ctx, 0, -area.size.height);
    
    
    
    CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
    
    
    
    CGContextSetAlpha(ctx, alpha);
    
    
    
    CGContextDrawImage(ctx, area, image.CGImage);
    
    
    
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    
    
    UIGraphicsEndImageContext();
    
    
    
    return newImage;
    
}
-(void)resetView{
    _picBtn.hidden = YES;
    _picImgBtn.hidden = YES;
    _imgView.hidden = YES;
    _line4.hidden = YES;
    [_alert mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_line5.mas_bottom).offset(ScaleSize(30));
        make.left.equalTo(_lsImgView.mas_left);
        make.right.equalTo(self.mas_right).offset(-5);
        make.height.mas_equalTo(@28);
        
    }];

}


-(void)clickSign:(UIButton*)sender{

    if ([_signDelegate respondsToSelector:@selector(clickSignBtn)]) {
        [_signDelegate clickSignBtn];
    }
    
}
-(void)upPic{
    if ([_signDelegate respondsToSelector:@selector(uploadPic)]) {
        [_signDelegate uploadPic];
    }
    
}


@end
